package com.paomi.client.activity.pay.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.paomi.client.R;
import com.paomi.client.activity.takeTicket.model.TakeTicketBean;
import com.paomi.client.utils.Util;

import java.util.ArrayList;
import java.util.List;

public class FlowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<TakeTicketBean> list = new ArrayList<>();
    private Context context;
    private int pos;
    private int type;

    public FlowAdapter(Activity context, int pos, int type) {
        this.context = context;
        this.pos = pos;
        this.type = type;
    }

    public void setData(List<TakeTicketBean> data) {
        this.list = data;
        notifyDataSetChanged();
    }

    OnItemClick itemClick;

    public void OnItemClick(OnItemClick onDeleteItemClick) {
        this.itemClick = onDeleteItemClick;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyHolder(View.inflate(context, R.layout.item_sku_item, null));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        TextView textView = ((MyHolder) holder).text;

        final TakeTicketBean des = list.get(position);
        if (des.isSelect()) {
            textView.setBackground(context.getResources().getDrawable(R.drawable.sub_btn_ischeck));
            textView.setTextColor(context.getResources().getColor(R.color.main_color));
        } else {
            textView.setBackground(context.getResources().getDrawable(R.drawable.sku_btn_uncheck));
            textView.setTextColor(context.getResources().getColor(R.color.color999999));
        }
        textView.setText(des.getName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int selPos = 0;
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).isSelect()) {
                        selPos++;
                    }
                }
                if (des.isSelect()) {
                    des.setSelect(false);
                    if (itemClick != null) {
                        itemClick.itemDelClick(des);
                    }
                } else {
                    if (type == 0) {
//                        Util.toast(context, "此项目只需" + 1 + "位购票人认证");
                        if(selPos>0){
                            Util.toast(context, "此项目只需" + 1 + "位购票人认证");
                            return;
                        }
                    } else {
                        if(pos==1){//判断只有一张票的时候
                            if(selPos > 0){//购票人不能多
                                Util.toast(context, "一张票只需" + 1 + "位购票人认证");
                                return;
                            }
                        }
                        if (selPos >= pos) {
                            Util.toast(context, "此项目只需" + pos + "位购票人认证");
                            return;
                        }
                    }
                    des.setSelect(true);
                    if (itemClick != null) {
                        itemClick.itemAddClick(des);
                    }
                }
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {

        private TextView text;

        public MyHolder(View itemView) {
            super(itemView);
            text = (TextView) itemView.findViewById(R.id.rcommend_tv);
        }
    }

    public interface OnItemClick {
        void itemAddClick(TakeTicketBean des);

        void itemDelClick(TakeTicketBean des);
    }
}